//
// Created by spiro on 19-3-30.
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "socket_tool.h"
#include "../chapter-10/rio.h"

#define MAXLINE 255

int main(int argc, char **argv) {
    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;
    
    if (argc != 3) {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        return -1;
    }
    
    host = argv[1];
    port = atoi(argv[2]);
    
    clientfd = open_clientfd(host, port);
    if (clientfd < 0) {
        return -1;
    }

    rio_readinitb(&rio, clientfd);
    while (fgets(buf, sizeof(buf), stdin) != NULL) {
        rio_writen(clientfd, buf, strlen(buf));
        rio_readlineb(&rio, buf, sizeof(buf));
        fputs(buf, stdout);
    }
    close(clientfd);
    return 0;
}

